home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / archivers / xpk / xpk_source / xpkmaster / query.c < prev    next >
C/C++ Source or Header  |  1999-06-14  |  5KB  |  190 lines

  1. #ifndef XPKMASTER_QUERY_C
  2. #define XPKMASTER_QUERY_C
  3.  
  4. /* Routinesheader
  5.  
  6.     Name:        query.c
  7.     Main:        xpkmaster
  8.     Versionstring:    $VER: query.c 1.11 (29.04.1998)
  9.     Author:        SDI
  10.     Distribution:    Freeware
  11.     Description:    Implementation of XpkQuery
  12.  
  13.  1.0   06.10.96 : first real version
  14.  1.1   27.12.96 : removed 1.3 specific functions
  15.  1.2   09.03.97 : added USER mode
  16.  1.3   31.03.97 : made tag processing better, nearly rewritten all, removed
  17.      goto's this way
  18.  1.4   09.06.97 : fixed Enforcer hit, when library opening failed
  19.  1.5   19.12.97 : code cleanup
  20.  1.6   24.12.97 : no packmode brings default mode now
  21.  1.7   01.01.98 : USER information is a bit better now
  22.  1.8   21.02.98 : uses new style register definition
  23.  1.9   03.03.98 : fixed enforcer hits
  24.  1.10  26.03.98 : some optimizations
  25.  1.11  29.04.98 : fixed bug (compiler problem)
  26. */
  27.  
  28. #include <exec/memory.h>
  29. #include <proto/exec.h>
  30. #include <proto/dos.h>
  31. #include <proto/xpksub.h>
  32. #include <proto/utility.h>
  33. #include "xpkmaster.h"
  34. #include "texts.h"
  35.  
  36. /************************************************************************
  37.  *
  38.  *  Information query
  39.  *
  40.  */
  41.  
  42. static const struct XpkMode USERMode = { 0,100,0,0,0,50,500,500,0,"user"};
  43.  
  44. static struct XpkInfo USERInfo = { 1,0,0,1,"USER","User",
  45. 0, 0x55534552, XPKIF_PK_CHUNK|XPKIF_UP_CHUNK, DEFAULTCHUNKSIZE, 10,
  46. DEFAULTCHUNKSIZE,0,0,0,0,100,0,&USERMode,0,0,0,0,0,0};
  47.  
  48. ASM(LONG) LIBXpkQuery(REG(a0, struct TagItem *tags))
  49. {
  50.   struct TagItem    *ti, *ti2 = tags;
  51.   ULONG            packmode = 101,
  52.             packmethod = 0,
  53.               prefs = 1; /* use prefs, default is true */
  54.   LONG            error = 0;
  55.   struct XpkPackerInfo *pinfo = 0;
  56.   struct XpkPackerList *plist = 0;
  57.   struct XpkMode *    pmode = 0;
  58.   struct Library *    XpkSubBase = 0;
  59.   struct XpkInfo *    sinfo = 0;
  60.   UBYTE            libname[SUBLIBNAME_SIZE];
  61.  
  62. #ifdef DEBUG
  63.   DebugTagList("XpkQuery", tags);
  64. #endif
  65.  
  66.   while((ti = NextTagItem(&ti2)))
  67.   {
  68.     switch(ti->ti_Tag)
  69.     {
  70.     case XPK_PackersQuery: plist = (struct XpkPackerList *) ti->ti_Data;
  71.       break;
  72.     case XPK_ModeQuery: pmode = (struct XpkMode *) ti->ti_Data; break;
  73.     case XPK_PackerQuery: pinfo = (struct XpkPackerInfo *) ti->ti_Data;
  74.       break;
  75.     case XPK_PackMethod: packmethod = idfromname((STRPTR) ti->ti_Data);
  76.       break;
  77.     case XPK_PackMode: packmode = ti->ti_Data; break;
  78.     case XPK_Preferences: prefs = ti->ti_Data; break;
  79.     }
  80.   }
  81.  
  82.   if(plist)
  83.   {
  84.     struct FileInfoBlock *fib;
  85.  
  86.     if(!(fib = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, 0)))
  87.       error = XPKERR_NOMEM;
  88.     else
  89.     {
  90.       ULONG lock;
  91.  
  92.       memset(plist, 0, sizeof(struct XpkPackerList));
  93.       if(!(lock = Lock("LIBS:compressors", ACCESS_READ)))
  94.         error = XPKERR_NOINFO;
  95.       else
  96.       {
  97.         if(!Examine(lock, fib) || fib->fib_DirEntryType < 0)
  98.           error = XPKERR_NOINFO;
  99.         else
  100.         {
  101.       while(ExNext(lock, fib))
  102.       {
  103.         if(!strncmp("xpk", fib->fib_FileName, 3) &&
  104.             !strcmp(".library", fib->fib_FileName+7))
  105.         {
  106.           ULONG ID = idfromname(fib->fib_FileName+3), i;
  107.  
  108.               for(i = plist->xpl_NumPackers; i > 0 &&
  109.           *(ULONG *) plist->xpl_Packer[i - 1] > ID; i--)
  110.           *(ULONG *) plist->xpl_Packer[i] =
  111.           *(ULONG *) plist->xpl_Packer[i-1];
  112.           *(ULONG *) plist->xpl_Packer[i] = ID;
  113.  
  114.           if(++plist->xpl_NumPackers == MAXPACKERS)
  115.             break;
  116.         }
  117.       }
  118.           if(prefs) /* add USER mode */
  119.           {
  120.         if(plist->xpl_NumPackers == MAXPACKERS)
  121.           --plist->xpl_NumPackers;
  122.             *(ULONG *) plist->xpl_Packer[plist->xpl_NumPackers++] = USER_COOKIE;
  123.           }
  124.         }
  125.         UnLock(lock);
  126.       }
  127.       FreeDosObject(DOS_FIB, fib);
  128.     }
  129.   }
  130.   else if(packmethod)
  131.   {
  132.     if(packmethod == USER_COOKIE)
  133.     {
  134.       sinfo = &USERInfo;
  135.       sinfo->xi_Description = strings[TXT_USER_DESCRIPTION];
  136.     }
  137.     else
  138.     {
  139.       sprintf(libname, SUBLIBNAME_STRING, &packmethod);
  140.  
  141.       if(!(XpkSubBase = OpenLibrary(libname, 0)) ||
  142.       !(sinfo = XpksPackerInfo()))
  143.         error = XPKERR_MISSINGLIB;
  144.     }
  145.     
  146.     if(!error)
  147.     {
  148.       if(pinfo)
  149.       {
  150.         sprintf(pinfo->xpi_Name, sinfo->xi_Name);
  151.         sprintf(pinfo->xpi_LongName, sinfo->xi_LongName);
  152.         sprintf(pinfo->xpi_Description, sinfo->xi_Description);
  153.         pinfo->xpi_Flags = sinfo->xi_Flags;
  154.         pinfo->xpi_MaxChunk = sinfo->xi_MaxPkInChunk;
  155.         pinfo->xpi_DefChunk = sinfo->xi_DefPkInChunk;
  156.         pinfo->xpi_DefMode = sinfo->xi_DefMode;
  157.       }
  158.       else if(pmode)
  159.       {
  160.         struct XpkMode* m = sinfo->xi_ModeDesc;
  161.  
  162.         if(packmode == 101)
  163.           packmode = sinfo->xi_DefMode;
  164.  
  165.         while(m && m->xm_Upto < packmode)
  166.           m = m->xm_Next;
  167.  
  168.         if(m)
  169.         {
  170.           CopyMem(m, pmode, sizeof(struct XpkMode));
  171.           pmode->xm_Next = 0;
  172.         }
  173.         else
  174.           error = XPKERR_NOINFO;
  175.       }
  176.       else
  177.         error = XPKERR_BADPARAMS;
  178.     }
  179.     if(XpkSubBase)
  180.       CloseLibrary(XpkSubBase);
  181.   }
  182.   else
  183.     error = XPKERR_BADPARAMS;
  184.  
  185.   return parseerrortags(tags, error);
  186. }
  187.  
  188. #endif /* XPKMASTER_QUERY_C */
  189.  
  190.